import sys
import os
import time
import subprocess
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QSplitter, QTreeView, QTabWidget, QStatusBar, 
    QVBoxLayout, QWidget, QTextEdit, QGraphicsView, QGraphicsScene, QGraphicsRectItem,
    QToolBar, QMessageBox, QPushButton, QLabel, QMenu, 
    QInputDialog, QFileDialog, QDialog, QDialogButtonBox, QSizePolicy
)
from PyQt6.QtGui import (
    QIcon, QTextCharFormat, QColor, QFont, QPen, QBrush, QPainterPath, QFileSystemModel,
    QTextCursor, QPalette, QStandardItemModel, QStandardItem, QSyntaxHighlighter, 
    QRegularExpressionValidator, QAction, QPainter
)
from PyQt6.QtCore import (
    Qt, QRectF, QPointF, QModelIndex, QFileInfo, QSortFilterProxyModel,
    QRegularExpression, QTimer, QSize
)
import git
from git import Repo, GitCommandError, InvalidGitRepositoryError






class CodeEditor(QTextEdit):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFont(QFont("Consolas", 12))
        self.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap)
        self.highlighter = PythonHighlighter(self.document())
        
        # 设置深色主题
        palette = self.palette()
        palette.setColor(QPalette.ColorRole.Base, QColor("#1e1e1e"))
        palette.setColor(QPalette.ColorRole.Text, QColor("#d4d4d4"))
        self.setPalette(palette)
    
    def open_file(self, file_path):
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                self.setPlainText(f.read())
            return True
        except Exception as e:
            self.setPlainText(f"# 打开文件错误: {str(e)}")
            return False
    
    def save_file(self, file_path):
        try:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(self.toPlainText())
            return True
        except Exception as e:
            return False